package com.liruo.oauth2.server.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import javax.annotation.security.RolesAllowed;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:liruo
 * @Date:2023-06-13-17:11:26
 * @Desc
 */
@Tag(name = "测试OAuth2下的接口")
@RestController
@RequestMapping("/test/oauth2")
public class TestOAuth2Controller {
  @GetMapping
  public User getUser(){
    return (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
  }
  @RolesAllowed("user")
  @GetMapping("/RolesAllowed")
  public User getUserRolesAllowed(){
    return (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
  }
  @PreAuthorize("hasRole('user')")
  @GetMapping("/PreAuthorize")
  public User getUserPreAuthorize(){
    return (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
  }
}
